package com.lagou.edu.servlet;

import com.lagou.factory.AnnoApplicatonBeanFacotry;
import com.lagou.factory.ApplicatonBeanFacotry;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @author: wudong
 * @create: 2021-05-31 18:37
 **/
public class SpringServletListener implements ServletContextListener {
    public static final String SERVER_PATH = "spring.context.path";
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        String contextClass = servletContext.getInitParameter("contextClass");
        try {
            ApplicatonBeanFacotry applicatonBeanFacotry = new AnnoApplicatonBeanFacotry(Class.forName(contextClass));
            applicatonBeanFacotry.refresh();
            servletContext.setAttribute(SERVER_PATH,applicatonBeanFacotry);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
